package org.zn.note.redis;

import java.io.UnsupportedEncodingException;

public class Main {

    public static void main(String[] args) throws UnsupportedEncodingException {
        stringAndByte();
    }

    private static void stringAndByte() throws UnsupportedEncodingException {
        String s = "你好";
//         String [你好], utf-8 bytes [E4BDA0E5A5BD], gbk bytes [C4E3BAC3]
        System.out.printf("String [%s], utf-8 bytes [%s], gbk bytes [%s]\n", s,
                JposIsoUtil.hexString(s.getBytes("utf-8")),
                JposIsoUtil.hexString(s.getBytes("gbk")));
//         这里不强制转成byte会报错，比如 0xE4 默认是int类型的，是32位的，强转byte后仅取低(右)8位
        byte[] bytesUtf8 = {(byte) 0xE4, (byte) 0xBD, (byte) 0xA0, (byte) 0xE5, (byte) 0xA5, (byte) 0xBD};
        byte[] bytesGbk = {(byte) 0xC4, (byte) 0xE3, (byte) 0xBA, (byte) 0xC3};
//        bytes [e4bda0e5a5bd], utf8 string [你好]
//        bytes[c4e3bac3], gbk string[你好]
        System.out.printf("bytes [%s], utf8 string [%s]\nbytes[%s], gbk string[%s]",
                JposIsoUtil.byte2hex(bytesUtf8), new String(bytesUtf8, "utf-8"),
                JposIsoUtil.byte2hex(bytesGbk), new String(bytesGbk, "gbk"));
    }
}
